#include "../Hdr/Camera.h"
#include "../Hdr/Input.h"
#include <SDL/SDL_opengl.h>
#include <iostream>

#define CAM_SPEED 25

Eternal::Scene::Camera::Camera(Core::InputManager *input)
{
	InputManager = input;
}

Eternal::Scene::Camera::~Camera()
{
	
}

void Eternal::Scene::Camera::SetX(const float x)
{
	Position.x = x;
}

void Eternal::Scene::Camera::SetY(const float y)
{
	Position.y = y;
}

void Eternal::Scene::Camera::SetZ(const float z)
{
	Position.z = z;
}

float Eternal::Scene::Camera::GetX() const
{
	return Position.x;
}

float Eternal::Scene::Camera::GetY() const
{
	return Position.y;
}

float Eternal::Scene::Camera::GetZ() const
{
	return Position.z;
}

void Eternal::Scene::Camera::SetPosition(const float x, const float y, const float z)
{
	Position.x = x;
	Position.y = y;
	Position.z = z;
}

void Eternal::Scene::Camera::SetPosition(const Core::Vector3 &v)
{
	Position = v;
}

void Eternal::Scene::Camera::SetDirection(const float x, const float y)
{
	f_xAng = (x > 360) ? x - 360 : x;
	f_yAng = (y > 360) ? y - 360 : y;
}

void Eternal::Scene::Camera::MoveForward(const float speed)
{
	const static float PI = 3.14f;
	const float fx = (f_xAng / 180) * PI;
	
	Position.x -= (sinf(fx) / 2) * speed;
	Position.z += (cosf(fx) / 2) * speed;
}

void Eternal::Scene::Camera::BeginTransform() const
{	
	glPushMatrix();
	glRotatef(f_yAng, 1, 0, 0);
	glRotatef(f_xAng, 0, 1, 0);
	glTranslatef(Position.x, Position.y, Position.z);
}

void Eternal::Scene::Camera::EndTransform() const
{
	glPopMatrix();
}

void Eternal::Scene::Camera::UseMouseLook()
{
/*	 int curX = InputManager->GetMouseX();
	 int curY = InputManager->GetMouseY();*/

	if ( InputManager->IsKeyPress((eKey)'w') )	{
		f_yAng--;
	}
	else if ( InputManager->IsKeyPress((eKey)'s') )	{
		f_yAng++;
	}
	if ( InputManager->IsKeyPress((eKey)'a') )	{
		f_xAng -= 0.5f;
	}
	else if ( InputManager->IsKeyPress((eKey)'d') )	{
		f_xAng += 0.5f;
	}

}

Eternal::Core::BoundingBox *Eternal::Scene::Camera::GetCollisionBox()
{
	return &CollisionBox;
}

void Eternal::Scene::Camera::UseArrowsMove()
{
	if ( InputManager->IsKeyPress(KEY_LEFT) )
	{
		f_xAng -= 90;
		MoveForward(CAM_SPEED);
		f_xAng += 90;
	}
	else if ( InputManager->IsKeyPress(KEY_RIGHT) )
	{
		f_xAng += 90;
		MoveForward(CAM_SPEED);
		f_xAng -= 90;
	}
	
	if ( InputManager->IsKeyPress(KEY_UP) )
	{
		MoveForward(CAM_SPEED);
	}
	else if ( InputManager->IsKeyPress(KEY_DOWN) )
	{
		MoveForward(-CAM_SPEED);
	}
}